package model

import (
	"common/global"
	"time"
)

type BargainRecord struct {
	Id         int64     `gorm:"column:id;type:mediumint;comment:砍价记录ID;primaryKey;not null;" json:"id"`                      // 砍价记录ID
	OrderId    int64     `gorm:"column:order_id;type:bigint;comment:关联的砍价订单ID;not null;" json:"order_id"`                     // 关联的砍价订单ID
	ActivityId int64     `gorm:"column:activity_id;type:mediumint;comment:关联的砍价活动ID;not null;" json:"activity_id"`            // 关联的砍价活动ID
	ProductId  int64     `gorm:"column:product_id;type:mediumint;comment:关联的医疗商品ID;not null;" json:"product_id"`              // 关联的医疗商品ID
	UserId     int64     `gorm:"column:user_id;type:int;comment:砍价的用户ID;not null;" json:"user_id"`                            // 砍价的用户ID
	UserType   int64     `gorm:"column:user_type;type:tinyint;comment:用户类型：0=发起者，1=帮砍者;not null;default:0;" json:"user_type"` // 用户类型：0=发起者，1=帮砍者
	CutAmount  float32   `gorm:"column:cut_amount;type:decimal(8, 2);comment:本次砍价金额;not null;" json:"cut_amount"`             // 本次砍价金额
	CreatedAt  time.Time `gorm:"column:created_at;type:datetime;comment:创建时间;not null;" json:"created_at"`                    // 创建时间
}

func (m *BargainRecord) TableName() string {
	return "bargain_record"
}

func BargainRecordCreate(record *BargainRecord) error {
	return global.Db.Create(record).Error
}

func ExistRecord(orderId, userId int64) (result *BargainRecord, err error) {
	err = global.Db.Model(&BargainRecord{}).Where("order_id =? AND user_id =?", orderId, userId).First(&result).Error
	if err != nil {
		return nil, err
	}
	return result, nil
}

func GetBargainRecord(orderId int64) (result []*BargainRecord, err error) {
	err = global.Db.Model(&BargainRecord{}).Where("order_id =?", orderId).Find(&result).Error
	if err != nil {
		return nil, err
	}
	return result, nil
}
